//
//  MBProgress+.swift
//  suda
//
//  Created by youai on 2019/7/18.
//  Copyright © 2019年 youi. All rights reserved.
//

import Foundation
import MBProgressHUD


private weak var MBProgressHUD_Shared : MBProgressHUD? = nil
private var MBProgressHUD_Shared_counter = 0

extension MBProgressHUD {
    static func showOrIncrease(view: UIView=UIApplication.shared.keyWindow!,showText:String? = nil){
        objc_sync_enter(self)
        if MBProgressHUD_Shared == nil {
            if Thread.isMainThread {
                MBProgressHUD_Shared_counter = 1
                let progressView = MBProgressHUD.showAdded(to: view, animated: true)
                if showText != nil {
                    progressView.label.text = showText!
                }
                progressView.removeFromSuperViewOnHide = true
                MBProgressHUD_Shared = progressView
            }else{
                mainQueueAsync {
                    showOrIncrease(view: view)
                }
            }
        }else{
            MBProgressHUD_Shared_counter += 1
        }
        objc_sync_exit(self)
    }
    
    static func hideOrDecrease(){
        objc_sync_enter(self)
        MBProgressHUD_Shared_counter -= 1
        if MBProgressHUD_Shared_counter <= 0 {
            if Thread.isMainThread {
                MBProgressHUD_Shared?.hide(animated: true)
                MBProgressHUD_Shared_counter = 0
            }else{
                mainQueueAsync {
                    hideOrDecrease()
                }
            }
        }
        objc_sync_exit(self)
    }
    
    static func hideAndZero(){
        objc_sync_enter(self)
        if MBProgressHUD_Shared_counter == 0 {
            return
        }
        if !Thread.isMainThread {
            mainQueueAsync {
                hideOrDecrease()
            }
            return
        }
        MBProgressHUD_Shared?.hide(animated: true)
        MBProgressHUD_Shared_counter = 0
        objc_sync_exit(self)
    }
}

extension MBProgressHUD {
    static func showImages() {
        objc_sync_enter(self)
        if MBProgressHUD_Shared == nil {
            if Thread.isMainThread {
                MBProgressHUD_Shared_counter = 1
                let progressView = MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
                let animateGifView = UIImageView.init()
                var gifArray:[UIImage] = []
                for i in 1...6{
                    gifArray.append(UIImage(named: String(format:"%d",i))!)
                }
                animateGifView.animationImages = gifArray
                animateGifView.animationDuration = 1
                animateGifView.animationRepeatCount = 100
                animateGifView.startAnimating()
                //        animateGifView.autorelease = true
                
                progressView.customView = animateGifView
                progressView.mode = .customView
                progressView.removeFromSuperViewOnHide = true
                MBProgressHUD_Shared = progressView
            }else{
                mainQueueAsync {
                    showImages()
                }
            }
        }else{
            MBProgressHUD_Shared_counter += 1
        }
        objc_sync_exit(self)
    }
        
}
